#include "Canvas.h"

Canvas::Canvas(){
	_uid = -_uid; //set canvas uid to negative
	copcont = 0; //set components cont to zero
	//bkg = oslLoadImageFilePNG(BCK_IMG_PATH, OSL_IN_RAM | OSL_SWIZZLED, OSL_PF_8888);
	//oslUncacheImage(bkg);
	_cursltindx = -1;
	_uilock = false;
}

void Canvas::draw(){
	//oslDrawImageXY(bkg, 0, 0);
	for(int i = 0 ; i < copcont; i++){
		components[i]->draw();
	}
	oslSetFont(font);
	oslDrawString(PSP_SCREEN_WIDTH-250,PSP_SCREEN_HEIGHT- 25,"左肩键");
	oslDrawString(PSP_SCREEN_WIDTH-200+*totalpagenum*20,PSP_SCREEN_HEIGHT- 25,"右肩键");
	oslSetFont(latinfont);
	for(int i = 0 ; i < *totalpagenum; i++){
	  if(pagenum == i+1){
	    oslDrawStringf(PSP_SCREEN_WIDTH-200+20*i, PSP_SCREEN_HEIGHT- 35,"%i",i+1);
	  }else
	    oslDrawStringf(PSP_SCREEN_WIDTH-200+20*i, PSP_SCREEN_HEIGHT- 25,"%i",i+1);
  }
}

void Canvas::AddComponent(UIComponent* comp){
	if(copcont < MAX_COMP_NUM){
		components[copcont++] = comp;
	}
	
	if(_cursltindx < 0){
		_cursltindx = 0;
	}
}

void Canvas::onhover(){
	
}

void Canvas::onblur(){
	
}

void Canvas::onselect(){
	//_uilock = true;
}

Canvas::~Canvas(){
	delete[] components;
}

void Canvas::handlectrl(const OSL_CONTROLLER *ctrl){
	if (ctrl->pressed.up || ctrl->pressed.left){
     if(copcont > 0 && _cursltindx > -1 && !_uilock){
			components[_cursltindx]->onblur();
			_cursltindx = (_cursltindx-1+copcont)%copcont;
			components[_cursltindx]->onhover();
		}
  }
  if (ctrl->pressed.right || ctrl->pressed.down){
    if(copcont > 0 && _cursltindx > -1 && !_uilock){
			components[_cursltindx]->onblur();
			_cursltindx = (_cursltindx+1)%copcont;
			components[_cursltindx]->onhover();
		}
  }
  if(ctrl->pressed.circle){
      _uilock = true;
			components[_cursltindx]->onselect();
  }
  if(ctrl->pressed.square){
    	components[_cursltindx]->onblur();
			_uilock = false;
  }
}